/////////////////////////////////////////////////////////////////////////////
// 2023 -
//open source under Apache License Version 2.0
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <kmhdfs/client/native_rs_raw_encoder.h>

namespace Hdfs {
namespace Internal {

NativeRSRawEncoder::NativeRSRawEncoder(ErasureCoderOptions & coderOptions) :
    AbstractNativeRawEncoder(coderOptions) {
    std::unique_lock<std::shared_mutex> lock(mutex);
    initImpl(coderOptions.getNumDataUnits(),
             coderOptions.getNumParityUnits());
}

void NativeRSRawEncoder::performEncodeImpl(std::vector<std::shared_ptr<ByteBuffer>> & inputs, int inputOffsets[], int dataLen,
                                           std::vector<std::shared_ptr<ByteBuffer>> & outputs, int outputOffsets[]) {
    encodeImpl(inputs, inputOffsets, dataLen, outputs, outputOffsets);
}

void NativeRSRawEncoder::release() {
    std::unique_lock<std::shared_mutex> lock(mutex);
    destroyImpl();
}

void NativeRSRawEncoder::initImpl(int numDataUnits, int numParityUnits) {
    RSEncoder * rsEncoder = (RSEncoder *) malloc(sizeof(RSEncoder));
    memset(rsEncoder, 0, sizeof(*rsEncoder));
    initEncoder(&rsEncoder->encoder, numDataUnits, numParityUnits, static_cast<bool>(isAllowVerboseDump()));
    nativeCoder = (void *)rsEncoder;
}

void NativeRSRawEncoder::encodeImpl(std::vector<std::shared_ptr<ByteBuffer>> & inputs, int inputOffsets[],
                                    int dataLen, std::vector<std::shared_ptr<ByteBuffer>> & outputs,
                                    int outputOffsets[]) {
    RSEncoder * rsEncoder = (RSEncoder *)nativeCoder;
    if (!rsEncoder) {
        THROW(HdfsException, "NativeRSRawEncoder closed");
    }
    int numDataUnits = rsEncoder->encoder.coder.numDataUnits;
    int numParityUnits = rsEncoder->encoder.coder.numParityUnits;
    int chunkSize = dataLen;

    // getInputs
    int numInputs = inputs.size();
    if (numInputs != numDataUnits) {
        THROW(HdfsException, "Invalid inputs");
    }
    std::shared_ptr<ByteBuffer> byteBuffer;
    for (int i = 0; i < numInputs; i++) {
        byteBuffer = inputs[i];
        if (byteBuffer != nullptr) {
            rsEncoder->inputs[i] = (unsigned char *)(byteBuffer->getBuffer());
            rsEncoder->inputs[i] += inputOffsets[i];
        } else {
            rsEncoder->inputs[i] = nullptr;
        }
    }

    // getOutputs
    int numOutputs = outputs.size();
    if (numOutputs != numParityUnits) {
        THROW(HdfsException, "Invalid outputs");
    }
    for (int i = 0; i < numOutputs; i++) {
        byteBuffer = outputs[i];
        rsEncoder->outputs[i] = (unsigned char *)(byteBuffer->getBuffer());
        rsEncoder->outputs[i] += outputOffsets[i];
    }

    // encode
    innerEncode(&(rsEncoder->encoder), rsEncoder->inputs, rsEncoder->outputs, chunkSize);
}

void NativeRSRawEncoder::destroyImpl() {
    RSEncoder * rsEncoder = (RSEncoder *)nativeCoder;
    if (rsEncoder) {
        free(rsEncoder);
        nativeCoder = nullptr;
    }
}

}
}